package fr.utbm.gi.in56.ads;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.utbm.gi.in56.database.DatabaseServlet;
import fr.utbm.gi.in56.error.ErrorType;
import fr.utbm.gi.in56.vehicle.BrandEntry;
import fr.utbm.gi.in56.vehicle.ModelEntry;
import fr.utbm.gi.in56.vehicle.VehicleManager;

/**
 * Servlet implementation class PostAdServlet
 * 
 * @author Adrien Hoffer & Tarik Belabbas
 */
@WebServlet("/PostAdServlet")
public class PostAdServlet extends AdServlet
{
	/**
	 * Version number associated to the serializable class.
	 */
	private static final long	serialVersionUID	= 1L;

	/**
	 * Default constructor.
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public PostAdServlet()
	{
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{		
		ErrorType result = forwardEnergyAndCarLists( request, response );
		if ( result != ErrorType.NONE )
		{
			request.setAttribute( "error", result ); //$NON-NLS-1$
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp"); //$NON-NLS-1$
			rd.forward( request, response );
		}
		
		String adType = request.getParameter( "adType" );
		if( adType.compareTo( "sell" ) == 0 )
		{
			RequestDispatcher rd = request.getRequestDispatcher( "postSellAd.jsp" ); //$NON-NLS-1$
			rd.forward( request, response );
			return;
		}
		else if( adType.compareTo( "buy" ) == 0 )
		{
			RequestDispatcher rd = request.getRequestDispatcher( "postBuyAd.jsp" ); //$NON-NLS-1$
			rd.forward( request, response );
			return;
		}
	}
}
